﻿namespace TweetsRetrieval
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Web.Helpers;
    using System.Threading;

    class Program
    {
        static void Main(string[] args)
        {
            WebClient webClient = new WebClient();

            StreamWriter outfilePositive = new StreamWriter(@"..\..\TweetsCorpus.pos.txt", true);
            StreamWriter outfileNegative = new StreamWriter(@"..\..\TweetsCorpus.neg.txt", true);
            StreamWriter tweets = new StreamWriter(@"..\..\TweetsCorpus.txt", true);
            
            List<string> positiveTweetsList = new List<string>();
            List<string> negativeTweetsList = new List<string>();


            var positiveTweetsCount = 0;
            var negativeTweetsCount = 0;
            var requests = 0;

            while (positiveTweetsCount < 800 && negativeTweetsCount < 800)
            {
                var jsonResponse = webClient.DownloadString("http://search.twitter.com/search.json?q=coke OR coca-cola&lang=en");
                
                var response = Json.Decode(jsonResponse);

                var tweetsToClassify = "{\"data\": [";
                bool notFirst = false;
                foreach (var tweet in response.results)
                {
                    if (notFirst) { tweetsToClassify += ","; }
                    notFirst = true;                    
                    tweetsToClassify += "{\"text\": \"" + tweet.text + "\"}";                    
                }
                tweetsToClassify += "]}";

                var classifiedTweetsResponse = webClient.UploadString("http://partners-v1.twittersentiment.appspot.com/api/bulkClassifyJson", tweetsToClassify);

                var classifiedTweets = Json.Decode(classifiedTweetsResponse);                
                
                foreach (var tweet in classifiedTweets.data)
                {
                    if (tweet.polarity == 0 && !negativeTweetsList.Contains(tweet.text)) // Negative tweets
                    {                        
                        outfileNegative.WriteLine(tweet.text);
                        outfileNegative.Flush();
                        negativeTweetsList.Add(tweet.text);
                        negativeTweetsCount++;
                        
                    }
                    else if (tweet.polarity == 4 && !positiveTweetsList.Contains(tweet.text)) // Positive tweets
                    {
                        outfilePositive.WriteLine(tweet.text);
                        outfilePositive.Flush();
                        positiveTweetsList.Add(tweet.text);
                        positiveTweetsCount++;
                    }
                    else
                    {
                        tweets.WriteLine(tweet.text);
                        tweets.Flush();
                    }
                }

                requests++;
                Console.Clear();
                Console.WriteLine("Requests: " + requests);

                Thread.Sleep(40000);
            }
        }
    }
}
